package com.shuqing.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shuqing.dao.BookMapper;
import com.shuqing.domain.Book;
import com.shuqing.domain.Record;
import com.shuqing.domain.User;
import com.shuqing.service.BookService;
import com.shuqing.service.RecordService;
import com.shuqing.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private RecordService recordService;

    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> books = bookMapper.selectNewBooks();
        return new PageResult(books.getTotal(),
                books.getResult());
    }

    @Override
    public Book findById(Integer id) {
        return bookMapper.findById(id);
    }



    /**
     *  借阅图书
     *  @param book
     *  @return
     */
    @Override
    public Integer borrowBook(Book book) {
        Book b = this.findById(book.getId());
        //格式化日期为yyyy-MM-dd格式
        SimpleDateFormat dateFormat = new
                SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(dateFormat.format(new Date()));
        //设置借阅状态为已借阅
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        //设置分页查询参数，开始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page = bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer addBook(Book book) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setUploadTime(dateFormat.format(new Date()));
        return bookMapper.addBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page;
        book.setBorrower(user.getName());
        if(user.getRole().equals("ADMIN")){
            page = bookMapper.selectBorrowed(book);
        }else {
            page = bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public boolean returnBook(Integer id, User user) {
//        根据图书ID获取图书信息
        Book book = this.findById(id);
//        判断借阅者和当前用户是否是一个人
        boolean isOneUser = user.getName().equals(book.getBorrower());
//        如果是 那么就修改
        if (isOneUser){
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return false;
    }

    /**
     * 确认归还
     * @param id
     * @return
     */
    @Override
    public Integer returnConfirm(Integer id) {
        Book book = this.findById(id);

//        保存记录
        Record record = this.setRecord(book);

//        把需要清楚的信息设置为0，图书改为可借阅状态
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer res = bookMapper.editBook(book);
        if(res == 1){
            return recordService.addRecord(record);
        }
        return 0;
    }

//    存储数据
    private Record setRecord(Book book){
        Record record = new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setRemandTime(book.getReturnTime());
        record.setBorrowTime(book.getBorrowTime());
//        设置图书归还确认时间为图书归还时间，并写入记录表
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        record.setBorrowTime(dateFormat.format(new Date()));
        return record;
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }


}
